//# sourceURL=http://uMapApp/Module/Bridge/BridgeWeatherDetectionModule
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../Module"
    , "../../UI/Panel/FloatPanel"
    , "../../UI/Panel/PanelBlock/ListBlock"
    , "../../UI/Panel/PanelBlock/PropertiesBlock"
    , "../../UI/Panel/PanelBlock/ChartBlock"
    , "../../UI/Panel/PanelBlock/TableBlock"
    , "../../UI/Panel/PanelBlock/TreeBlock"
    , "../../UI/Tree/Tree"
    , "../../UI/Panel/Item/ListItem"
    , "./BridgeWeatherDetection"
    , "./BridgeWeatherDetectionNodeBlock"
    , "../../UI/Checkbox"
    , "../../UI/Panel/Panel"
], function (
    mhjs
    , uMap
    , uEarth
    , Module
    , FloatPanel
    , ListBlock
    , PropertiesBlock
    , ChartBlock
    , TableBlock
    , TreeBlock
    , Tree
    , ListItem
    , BridgeWeatherDetection
    , BridgeWeatherDetectionNodeBlock
    , Checkbox
    , Panel
) {

    function BridgeWeatherDetectionModule(option) {

        var _this = this;

        Module.call(_this, option);

        _this.title = "桥梁气象";
        _this.icon = _this.app.icons.bridge;
        _this.panels = [];
        _this.items = [];

        _this.listPanel = new FloatPanel({"class": "rightPanel", "showMinMax": true, "title": "桥梁列表"});
        _this.listPanel.listBlock = new ListBlock({"class": "fullBlock"});
        _this.listPanel.listBlock.title = "桥梁气象监测点";
        _this.listPanel.append(_this.listPanel.listBlock);

        _this.listPanel.on("close", function () {
            _this.closePanel();
        });

        _this.panels.push(_this.listPanel);

        _this.chartPanel = new FloatPanel({
            "class": "leftPanel BridgeWeatherDetectionModule chartPanel", "showMinMax": true, "title": "太浦河特大桥桥面气象监测数据展示与分析"
        });
        _this.chartPanel.chartBlock = new ChartBlock({
            "title": "太浦河特大桥桥面气象监测数据展示与分析",
            "chart": {
                "height"     : 270,
                "width"      : 740,
                "chartOption": {
                    xAxis  : {
                        type       : "category",
                        boundaryGap: false
                    },
                    yAxis  : {
                        type: "value"
                    },
                    series : {
                        type: "line"
                    },
                    grid   : {
                        top         : "10%",
                        left        : "0%",
                        right       : "0%",
                        bottom      : "0%",
                        containLabel: true
                    },
                    tooltip: {
                        trigger        : "axis",
                        backgroundColor: "rgb(1, 20, 30)",
                        borderColor    : "#333",
                        textStyle      : {
                            color: "#fff"
                        },
                        confine        : true
                    }
                }
            }
        });

        _this.chartPanel.tableBlock = new TableBlock();

        _this.chartPanel.tableBlock.ui.title.hide();
        _this.chartPanel.tableBlock.ui.filterBlock.hide();

        _this.chartPanel
            .append(_this.chartPanel.chartBlock)
            .append(_this.chartPanel.tableBlock)
        ;

        _this.chartPanel.on("close", function () {
            if (_this.chartPanel.bridgeWeatherDetectionNode) {
                _this.chartPanel.bridgeWeatherDetectionNode.bridgeWeatherDetection.unActiveAllNode();
            }
        });

        _this.panels.push(_this.chartPanel);

        _this.alarmPanel = new FloatPanel({
            "class": "leftPanel BridgeWeatherDetectionModule chartPanel", "showClose": true
        });

        _this.alarmPanel.tableBlock = new TableBlock();

        _this.alarmPanel.tableBlock.title = "太浦河特大桥桥面气象监测告警";

        // _this.alarmPanel.tableBlock.ui.title.hide();
        // _this.alarmPanel.tableBlock.ui.filterBlock.hide();

        _this.alarmPanel.tableBlock.head = [
            {
                "label" : "序号",
                "col"   : {"width": 50},
                "format": function (tableBlock, td) {
                    td.text(tableBlock.body.length + 1);
                }
            },
            {
                "value": "name",
                "label": "设备名称"
            },
            {
                "value": "alarmSubType",
                "label": "告警名称"
            },
            {
                "value": "alarmType",
                "label": "告警类型"
            },
            {
                "value": "alarmStatus",
                "label": "告警状态"
            },
            {
                "value": "alarmValue",
                "label": "状态数值"
            },
            {
                "value": "time",
                "label": "采集时间"
            }
        ];

        _this.alarmPanel
            .append(_this.alarmPanel.tableBlock)
        ;

        _this.alarmPanel.on("close", function () {
            if (_this.alarmPanel.bridgeWeatherDetectionNode) {
                _this.alarmPanel.bridgeWeatherDetectionNode.bridgeWeatherDetection.unActiveAllNode();
            }
        });

        _this.panels.push(_this.alarmPanel);

        _this.nodeListPanel = new FloatPanel({"class": "rightPanel BridgeWeatherDetectionModule", "showClose": true});
        _this.nodeListPanel.treeBlock = new TreeBlock({"class": "fullBlock"});

        // _this.nodeListPanel.treeBlock.tree.status = _this.nodeListPanel.treeBlock.tree.add(new Tree({
        //     "title": "状态",
        //     "list" : []
        // }));
        // _this.nodeListPanel.treeBlock.tree.types = _this.nodeListPanel.treeBlock.tree.add(new Tree({
        //     "title": "类型",
        //     "list" : []
        // }));

        _this.nodeListPanel.treeBlock.title = "桥梁气象监测点";
        _this.nodeListPanel.append(_this.nodeListPanel.treeBlock);

        _this.nodeListPanel.on("close", function () {
            if (_this.nodeListPanel.bridgeWeatherDetection) {
                _this.closeNode(_this.nodeListPanel.bridgeWeatherDetection);
            }
        });

        _this.panels.push(_this.nodeListPanel);

        _this.totalPanel = new Panel({"class": "scModule totalPanel noStyle"});

        _this.totalPanel.detectionCount = _this.renderShCountBlockItem({
            "title": "监测桥梁数量",
            "count": 1,
            "unit" : "座"
        });

        _this.totalPanel.detectionNodeCount = _this.renderShCountBlockItem({
            "title": "监测点数量",
            "count": 2,
            "unit" : "个"
        });


        _this.totalPanel
            .append(_this.totalPanel.detectionCount)
            .append(_this.totalPanel.detectionNodeCount)
        ;

        _this.panels.push(_this.totalPanel);

        _this.app.ui.layout.top.append(_this.totalPanel);
        _this.app.ui.layout.right.append(_this.listPanel);
        _this.app.ui.layout.right.append(_this.nodeListPanel);
        _this.app.ui.layout.left.append(_this.chartPanel);
        _this.app.ui.layout.left.append(_this.alarmPanel);
        // _this.app.ui.layout.left.append(_this.detailPanel);
    }

    var _class = BridgeWeatherDetectionModule;

    mhjs.Inherit(_class, Module);

    Object.defineProperties(_class.prototype, {
        "data"                  : {
            set: function (data) {
                var _this = this;
                _this.clear();
                _this._data = data;
                data && _this.renderList(data);
            },
            get: function () {
                return this._data;
            }
        },
        "renderList"            : {
            value: function (list) {

                var _this = this;

                _this.bridges = {};

                $.each(list, function (i, item) {

                    if (_this.objMap) {
                        item = _this.objMap.format(item);
                    }

                    item.app = _this.app;

                    item.nodeRequest = _this.node.request;
                    item.nodeDataRequest = _this.node.dataRequest;
                    item.nodePropertiesRequest = _this.node.propertiesRequest;
                    item.nodeMap = _this.node.map;
                    item.module = _this;

                    var bridgeWeatherDetection = new BridgeWeatherDetection(item);

                    _this.items.push(bridgeWeatherDetection);

                    _this.bridges[bridgeWeatherDetection["编号"]] = bridgeWeatherDetection;

                    bridgeWeatherDetection.listItem = new ListItem({
                        "title"                 : bridgeWeatherDetection["名称"],
                        "subtitle"              : bridgeWeatherDetection["类型"],
                        "icon"                  : _this.icon.base64,
                        "bridgeWeatherDetection": bridgeWeatherDetection
                    });


                    bridgeWeatherDetection.marker.on("click", function () {
                        _this.openNode(bridgeWeatherDetection);
                    });

                    bridgeWeatherDetection.listItem.on("click", function () {
                        _this.openNode(bridgeWeatherDetection);
                    });

                    bridgeWeatherDetection.listItem.on("hide", function (listItem) {
                        listItem.bridgeWeatherDetection.close();
                    });

                    bridgeWeatherDetection.listItem.on("show", function (listItem) {
                        listItem.bridgeWeatherDetection.open();
                    });

                    _this.listPanel.listBlock.add(bridgeWeatherDetection.listItem);

                });

                _this.listPanel.listBlock.filter = {
                    "param": "bridgeWeatherDetection.searchKey"
                };

                _this.listPanel.listBlock.sorter = {
                    "param": {
                        "value": "bridgeWeatherDetection.sortKey",
                        "label": "桩号"
                    }
                };
            }
        },
        "openNode"              : {
            value: function (bridgeWeatherDetection) {

                var _this = this;

                _this.isOpened && _this.totalPanel.close(false);

                $.each(_this.items, function (i, item) {
                    item !== bridgeWeatherDetection && item.close();
                });

                bridgeWeatherDetection.layer.active();

                bridgeWeatherDetection.openNode(function () {

                    _this.nodeListPanel.treeBlock.tree.clear();
                    // _this.nodeListPanel.treeBlock.tree.status.clear();
                    // _this.nodeListPanel.treeBlock.tree.types.clear();

                    // _this.nodeListPanel.treeBlock.tree.status.classNames = {};
                    // _this.nodeListPanel.treeBlock.tree.types.classNames = {};

                    // $.each(bridgeWeatherDetection.nodeDic.status, function (i, statusType) {
                    //
                    //     _this.nodeListPanel.treeBlock.tree.status.classNames[statusType.className] = _this.nodeListPanel.treeBlock.tree.status.add(new Tree({
                    //         "title": statusType.label,
                    //         "list" : [],
                    //         "class": statusType.className
                    //     }));
                    // });

                    // _this.nodeListPanel.treeBlock.tree.status.title = "状态 ( " + _this.nodeListPanel.treeBlock.tree.status.list.length + " )";
                    // _this.nodeListPanel.treeBlock.tree.status.openChild();


                    // $.each(bridgeWeatherDetection.nodeDic.type, function (i, typeType) {
                    //
                    //     _this.nodeListPanel.treeBlock.tree.types.classNames[typeType.className] = _this.nodeListPanel.treeBlock.tree.types.add(new Tree({
                    //         "title": typeType.label,
                    //         "list" : [],
                    //         "class": typeType.className
                    //     }));
                    //
                    // });

                    // _this.nodeListPanel.treeBlock.tree.types.title = "类型 ( " + _this.nodeListPanel.treeBlock.tree.types.list.length + " )";
                    // _this.nodeListPanel.treeBlock.tree.types.openChild();

                    $.each(bridgeWeatherDetection.nodes, function (i, bridgeWeatherDetectionNode) {

                        if (!bridgeWeatherDetectionNode.statusTree) {

                            bridgeWeatherDetectionNode.statusTree = new Tree({
                                "title": bridgeWeatherDetectionNode.title,
                                // "class": bridgeWeatherDetectionNode.status.className
                                "class": "normal"
                            });

                            // bridgeWeatherDetectionNode.typeTree = new Tree({
                            //     "title": bridgeWeatherDetectionNode.title,
                            //     "class": bridgeWeatherDetectionNode.type.className
                            // });

                            bridgeWeatherDetectionNode.propertiesBlock = new BridgeWeatherDetectionNodeBlock();

                            bridgeWeatherDetectionNode.loadProperties(function () {

                                bridgeWeatherDetectionNode.on("click", function () {
                                    bridgeWeatherDetectionNode.bridgeWeatherDetection.activeNode(bridgeWeatherDetectionNode);
                                    _this.app.pop.target = bridgeWeatherDetectionNode;
                                    _this.app.pop.position = bridgeWeatherDetectionNode.position;
                                    bridgeWeatherDetectionNode.propertiesBlock.properties = bridgeWeatherDetectionNode;
                                    _this.app.pop.content = bridgeWeatherDetectionNode.propertiesBlock;
                                    _this.app.pop.open();

                                });

                            });

                            bridgeWeatherDetectionNode.on("propertiesButtonClick", function () {
                                _this.app.pop.close();
                                _this.openNodeData(bridgeWeatherDetectionNode);
                            });

                            bridgeWeatherDetectionNode.on("alarmButtonClick", function () {
                                _this.app.pop.close();
                                _this.openNodeAlarm(bridgeWeatherDetectionNode);
                            });

                            bridgeWeatherDetectionNode.statusTree.on("click", function () {
                                bridgeWeatherDetectionNode.bridgeWeatherDetection.activeNode(bridgeWeatherDetectionNode);
                            });

                            // bridgeWeatherDetectionNode.typeTree.on("click", function () {
                            //     bridgeWeatherDetectionNode.bridgeWeatherDetection.activeNode(bridgeWeatherDetectionNode);
                            // });

                        }

                        _this.nodeListPanel.treeBlock.tree.add(bridgeWeatherDetectionNode.statusTree);
                        // _this.nodeListPanel.treeBlock.tree.status.classNames[bridgeWeatherDetectionNode.status.className].add(bridgeWeatherDetectionNode.statusTree);
                        // _this.nodeListPanel.treeBlock.tree.types.classNames[bridgeWeatherDetectionNode.type.className].add(bridgeWeatherDetectionNode.typeTree);

                    });


                    // $.each(_this.nodeListPanel.treeBlock.tree.status.list, function (i, subTree) {
                    //     subTree.title += (" ( " + subTree.list.length + " )");
                    //     subTree.openChild();
                    // });


                    // $.each(_this.nodeListPanel.treeBlock.tree.types.list, function (i, subTree) {
                    //     subTree.title += (" ( " + subTree.list.length + " )");
                    //     subTree.openChild();
                    // });

                    _this.nodeListPanel.bridgeWeatherDetection = bridgeWeatherDetection;

                    _this.nodeListPanel.open();

                    _this.trigger("openNode");

                });

            }
        },
        "openNodeData"          : {
            value: function (bridgeWeatherDetectionNode) {

                var _this = this;

                _this.chartPanel.open(false);
                _this.chartPanel.max(false);
                // _this.chartPanel.chartBlock.title = bridgeWeatherDetectionNode.title;
                _this.chartPanel.tableBlock.clear();

                var startTime = new Date(new Date().getTime() - 7 * 24 * 60 * 60 * 1000).format("yyyy-MM-dd");
                var endTime = new Date().format("yyyy-MM-dd");

                bridgeWeatherDetectionNode.loadData(startTime, endTime, function () {

                    var date = [];
                    var series = [];
                    var seriesData = {};
                    var legend = {data: []};
                    var tabHead = [];
                    var tr = {};

                    // {
                    //     "timestamp": "2025-01-11T23:59:40.431108854",
                    //     "data": {
                    //     "路面温度": {
                    //         "value": "0.6",
                    //             "unit": "℃"
                    //     },
                    //     "路面积水厚度": {
                    //         "value": "0",
                    //             "unit": "mm"
                    //     },
                    //     "路面覆冰厚度": {
                    //         "value": "0",
                    //             "unit": "mm"
                    //     },
                    //     "路面积雪厚度": {
                    //         "value": "0",
                    //             "unit": "mm"
                    //     },
                    //     "路面湿滑程度": {
                    //         "value": "0",
                    //             "unit": ""
                    //     },
                    //     "电量": {
                    //         "value": "65",
                    //             "unit": "%"
                    //     }
                    // }


                    $.each(bridgeWeatherDetectionNode.data, function (i, dataItem) {

                        var timestame = new Date(dataItem.timestamp).format("yyyy-MM-dd hh:mm");

                        date.push(timestame);

                        tr.time = timestame;

                        if (legend.data.length === 0) {
                            $.each(dataItem.data, function (label, dataO1) {
                                legend.data.push(label);
                                seriesData[label] = [];
                                tabHead.push({
                                    "value": label,
                                    "label": label + (dataO1.unit ? "(" + dataO1.unit + ")" : "")
                                });
                            });

                            _this.chartPanel.tableBlock.head = [
                                {
                                    "label" : "序号",
                                    "col"   : {"width": 50},
                                    "format": function (tableBlock, td) {
                                        td.text(tableBlock.body.length + 1);
                                    }
                                },
                                {
                                    "value": "time",
                                    "label": "采集时间"
                                }
                            ].concat(tabHead);

                        }

                        $.each(dataItem.data, function (label, dataO2) {
                            seriesData[label].push(dataO2.value);
                            tr[label] = dataO2.value;
                        });

                        _this.chartPanel.tableBlock.add(tr);

                    });

                    $.each(seriesData, function (label, data) {
                        series.push({
                            name: label,
                            type: "line",
                            data: data
                        });
                    });

                    _this.chartPanel.chartBlock.chart.setOption({
                        xAxis : {
                            data: date
                        },
                        legend: legend,
                        series: series
                    });

                    _this.chartPanel.bridgeWeatherDetectionNode = bridgeWeatherDetectionNode;

                });

            }
        },
        "openNodeAlarm"         : {
            value: function (bridgeWeatherDetectionNode) {

                var _this = this;

                _this.alarmPanel.open(false);
                _this.alarmPanel.tableBlock.clear();

                //未生效
                var startTime = new Date(new Date().getTime() - 7 * 24 * 60 * 60 * 1000).format("yyyy-MM-dd");
                var endTime = new Date().format("yyyy-MM-dd");

                bridgeWeatherDetectionNode.loadStatusAlert(startTime, endTime, function () {

                    bridgeWeatherDetectionNode.loadThresholdAlert(startTime, endTime, function () {

                        // {
                        //     "timestamp": "2025-01-05T09:06:26.558624696",
                        //     "alarmType": "status",
                        //     "alarmSubType": "设备状态",
                        //     "alarmStatus": "其他警告"
                        // }

                        var totalAlerts = bridgeWeatherDetectionNode.statusAlerts.concat(bridgeWeatherDetectionNode.thresholdAlerts).sort(
                            function (a, b) {
                                return new Date(b.timestamp).getTime() - new Date(a.timestamp).getTime();
                            }
                        );

                        console.log(totalAlerts);

                        $.each(totalAlerts, function (i, alarmItem) {
                            _this.alarmPanel.tableBlock.add({
                                "name"        : bridgeWeatherDetectionNode.title,
                                "alarmSubType": alarmItem.alarmSubType + "告警",
                                "alarmType"   : alarmItem.alarmType === "status" ? "状态告警" : "阈值告警",
                                "alarmStatus" : alarmItem.alarmStatus,
                                "alarmValue"  : alarmItem.alarmValue + alarmItem.alarmUnit,
                                "time"        : new Date(alarmItem.timestamp).format("yyyy-MM-dd hh:mm")
                            }, function (item, tr) {
                                if (item.alarmStatus !== "正常") {
                                    tr.addClass("error");
                                }
                            });
                        });

                        _this.alarmPanel.bridgeWeatherDetectionNode = bridgeWeatherDetectionNode;

                    });
                });
            }
        },
        "closeNode"             : {
            value: function (bridgeWeatherDetection) {

                var _this = this;

                _this.isOpened && _this.totalPanel.open(false);

                bridgeWeatherDetection.closeNode();
                _this.chartPanel.close(false);

                _this.app.flyHome();

                $.each(_this.items, function (i, item) {
                    item !== bridgeWeatherDetection && item.open();
                });

                _this.trigger("closeNode");

            }
        },
        "clear"                 : {
            value: function () {
                var _this = this;

                $.each(_this.items, function (i, item) {
                    item.close();
                    item.listItem.remove();
                });

                _this._data = undefined;
                _this.items = [];
            }
        },
        "open"                  : {
            value: function (forceReload) {
                var _this = this;

                _this.loadInit(function () {

                    if (_this.app.main3DLayer) {
                        _this.sysMain3DLayerOpened = _this.app.main3DLayer.isOpened;
                        _this.app.ui.elements.tileSwitchPanel.tileSwitch.isChecked = false;
                    }

                    $.each(_this.items, function (i, item) {
                        item.open();
                    });

                    _this.listPanel.open(false);
                    _this.totalPanel.open();

                    _this.isOpened = true;

                    _this.trigger("open");
                    _this.trigger("toggle");
                }, forceReload);


            }
        },
        "loadInit"              : {
            value: function (callback, forceReload) {

                var _this = this;

                if (forceReload || !_this.isLoaded) {

                    _this.load({}, function (data, mapData) {

                        if (mapData) {
                            _this.objMap = mapData.obj;
                            _this.propertiesMap = mapData.properties;
                            _this.dic = mapData.dic;
                        }

                        _this.data = data;
                        _this.isLoaded = true;
                        callback && callback();


                    });

                } else {
                    callback && callback();
                }


            }
        },
        "openPanel"             : {
            value: function () {
                var _this = this;
                if (!_this.isOpened) {
                    _this.open();
                }

                _this.listPanel.open();
                _this.isPanelOpened = true;
                _this.trigger("openPanel");

            }
        },
        "close"                 : {
            value: function () {
                var _this = this;

                if (_this.sysMain3DLayerOpened) {
                    _this.app.ui.elements.tileSwitchPanel.tileSwitch.isChecked = true;
                }

                _this.chartPanel.close();
                _this.nodeListPanel.close();
                _this.totalPanel.open();

                $.each(_this.items, function (i, item) {
                    item.close();
                });

                _this.closePanel();

                _this.isOpened = false;
                _this.trigger("close");
                _this.trigger("toggle");
            }
        },
        "closePanel"            : {
            value: function () {
                var _this = this;
                _this.closeAllPanel();
                _this.isPanelOpened = false;
                _this.trigger("closePanel");
            }
        },
        "activeItem"            : {
            value: function (item) {
                var _this = this;
                _this.unActiveAllItem();
                item.active(!item.isActive);
                item.listItem.toggleClass("active", item.isActive);
            }
        },
        "unActiveAllItem"       : {
            value: function () {
                var _this = this;
                $.each(_this.items, function (i, item) {
                    item.active(false);
                    item.listItem.removeClass("active");
                });
            }
        },
        "renderShCountBlockItem": {
            value: function (item) {
                var _this = this;

                var div = new mhjs.Div({"class": "item"});

                div.title = new mhjs.Div({"class": "title"});

                div.title.text(item.title);

                div.icon = new mhjs.Img({"class": "icon"});

                div.icon.attr("src", item.icon);

                div.count = new mhjs.Div({"class": "count"});

                div.count.text(item.count);

                div.count.unit = new mhjs.Span({"class": "unit"});

                div.count.unit.text(item.unit);

                div
                    // .append(div.icon)
                    .append(div.title)
                    .append(div.count
                        .append(div.count.unit)
                    )

                ;

                return div;

            }
        }
    });

    return _class;

});